<?php
/**     
  *
  * Copyright (c) 2009, Persistent Systems Limited
  *
  * Redistribution and use, with or without modification, are permitted 
  *  provided that the following  conditions are met:
  *   - Redistributions of source code must retain the above copyright notice, 
  *     this list of conditions and the following disclaimer.
  *   - Neither the name of Persistent Systems Limited nor the names of its contributors 
  *     may be used to endorse or promote products derived from this software 
  *     without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
/**
 * File path for DotNetServicesEnvironment.php
 */
require_once 'DotNetServicesEnvironment.php';
/**
 * File path for HttpWebProxy.php
 */
require_once 'util/HttpWebProxy.php';
/**
 * File path for HttpWebProxy.php
 */
require_once 'util/curlRequest.php';
/**
 * File path for Scope.php
 */
require_once 'ACS/Scope.php';
/**
 * File path for SimpleApiAuthService.php
 */
require_once 'ACS/SimpleApiAuthService.php'; 
/**
 * File path for Authentication.php
 */
//require_once 'ACS/Authentication.php'; 
/**
 * AuthenticationModel to do Authentication process
 * @access public
 */
class AuthenticationModel
{
	/** 
     * Class constructor.
	 * @access public
	 */
	public function __construct()
	{

	}
	/**
	 * This function validate user login details
	 * @access public
	 * @param array $userLoginInfo Username and password
	 * @return array $errorMsg Error messages if invalid login credentials provided
	 */
	public function validateUserLogin($userLoginInfo)
	{
		$errorMsg = array();
		try{
			$userName = trim($userLoginInfo['txtUsername']);
			$password = trim($userLoginInfo['txtPassword']);

			if ($userName == ''  || strlen($userName) < 0){
				$errorMsg[]= "Please enter your Scope name.";
			} 
			if ($password == '' || strlen($password) < 0){
				$errorMsg[]= "Please enter your Issuer key.";
			}			
			
		} catch (Exception $e) {
			throw($e);
		}
		return $errorMsg;
	}
	
	/**
	 * This function do ACS authentication returns token received from ACS
     * @access public
	 * @param string $serviceName Service Name
	 * @param string $issuerKey Issuer Key
	 * @return array $response Array of Token and errors
	 */
	public function authAcs($scopeName,$issuerKey)
	{
		$response=array('token'=>'','error'=>array());
		$acmHostName = DotNetServicesEnvironment::getACMHostName();
		
		try
		{
			
			$serviceName = SERVICE_NAMESPACE_DOMAIN;
			$acmHostName = DotNetServicesEnvironment::getACMHostName();
			
			$scope = new Scope('simpleAPIAuth');


			$scope->setIssuerName($scopeName);
			$scope->setIssuerSecret($issuerKey);
			$scope->setAppliesTo(SCOPE_APPLIES_TO);
			 
			$simpleApiAuthService = new SimpleApiAuthService($acmHostName, $serviceName);
			$simpleApiAuthService->setScope($scope);

			// Need this only from corpnet
			$httpWebProxy = new HttpWebProxy(HTTP_PROXY_HOST, HTTP_PROXY_PORT);
			$simpleApiAuthService->setHttpWebProxy($httpWebProxy);
			
		
			$requestinfo['acsHostName'] = $acmHostName;
			$requestinfo['scopeName'] = $scopeName;
			
			$token = $simpleApiAuthService->getACSToken();
		}
		catch(Exception $e)
		{
			if(strpos($e->getMessage(),"code 502")!== false) 
			{
				$errors[] = "Please check your Scope Name";
			}
			if(strpos($e->getMessage(),"sa_password")!== false)
			{
				$errors[] = "Please check your Issuer key";
			}
			$response['error'] = $errors;
		}
		$response['token'] = $token;
		return $response  ;
	}
}
?>